/*
 * Created by SharpDevelop.
 * User: fatman
 * Date: 10/27/2008
 * Time: 8:04 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;

namespace CUALib
{
	/// <summary>
	/// Description of MenuBarItem.
	/// </summary>
	public class MenuBarItem : MenuItem
	{
		public MenuBarItem(bool consoleExists): base(consoleExists)
		{
			this.Height=1;
			this.Width=1;
		}
		
		
		public override void Invalidate(System.Drawing.Rectangle rect)
		{
			if (!Visible)
				return; 
			Redraw();
			
		}
		
		
		internal override void Redraw()
		{
			if (Parent==null)
				return;

			
			ConsoleColor oldForeColor = Console.ForegroundColor;
			Console.ForegroundColor = ForegroundColor;
			ConsoleColor oldBackColor = Console.BackgroundColor;
			Console.BackgroundColor = BackgroundColor;
			Point oldCursorLocation = new Point(Console.CursorLeft, Console.CursorTop);
			
			
			
			Point pp;
			int _indexParent;
			_indexParent=Parent.Controls.IndexOf(this);
			if(_indexParent==0)
			{
				Location=Parent.Location;
				pp=Parent.Location;
			}
			else
			{
				Location=Parent.Controls[_indexParent-1].Location;
				Rectangle abp=Parent.Controls[_indexParent-1].AbsoluteBounds;
				pp=abp.Location;
				pp.Offset(abp.Width,0);
			}
			Console.SetCursorPosition(pp.X, pp.Y);
			Console.Write(" " + Text + " ");
			
			Console.ForegroundColor = oldForeColor;
			Console.BackgroundColor = oldBackColor;
			Console.SetCursorPosition(oldCursorLocation.X, oldCursorLocation.Y);
			
		}
		
		
		
		private string _text=string.Empty;
		public virtual string Text
		{
			get
			{
				return _text;
			}
			set
			{
				_text=value;
				Width=_text.Length+2;
			}
		}
		
		
		
		public override ConsoleControl Parent
		{
			get
			{
				return base.Parent;
			}
			set
			{
				if(!(value is MenuBar))
					throw new Exception("MenuBarItem's parent MUST be a MenuBar instance");
				base.Parent = value;
			}
		}
		
		private new MenuPanel _menuPanel;
		
		public override bool ReceiveEvent(ConsoleKeyInfo cki)
		{
			
			if (Focused && cki.Key== ConsoleKey.Enter)
			{
				if(_menuPanel==null)
					_menuPanel=new MenuPanel(true);
				//show dropdown menu
			}
			return base.ReceiveEvent(cki);
		}


	
	}
}
